/**
 * Copyright (c) 2011 Tomi Paananen.
 *
 * Part of Quick Gestures user interface.
 */

#ifndef GESTUREVISUALIZER_H
#define GESTUREVISUALIZER_H

// Qt includes
#include <QColor>
#include <QDeclarativeItem>
#include <QRectF>
#include <QTimer>

// qgengine includes
#include <gesturedatahelper.h>

// Forward declarations
class Gesture;


class GestureVisualizer : public QDeclarativeItem
{
    Q_OBJECT
    Q_PROPERTY(Gesture* gesture READ gesture WRITE setGesture NOTIFY gestureChanged)
    Q_PROPERTY(int contentWidth READ contentWidth NOTIFY contentWidthChanged)
    Q_PROPERTY(int contentHeight READ contentHeight NOTIFY contentHeightChanged)
    Q_PROPERTY(bool autoScale READ autoScale WRITE setAutoScale)
    Q_PROPERTY(int lineWidth READ lineWidth WRITE setLineWidth)
    Q_PROPERTY(QColor lineColor READ lineColor WRITE setLineColor)
    Q_PROPERTY(bool playing READ playing WRITE setPlaying NOTIFY playingChanged)

protected: // Data types

    enum AnimationStates {
        Stopped = 0,
        Paused,
        Playing
    };

public:
    GestureVisualizer(QDeclarativeItem *parent = 0);
    virtual ~GestureVisualizer();

public: // From QGraphicsItem
    void paint(QPainter *painter,
               const QStyleOptionGraphicsItem *option,
               QWidget *widget);

public: // Property setters and getters
    Gesture *gesture() const;
    void setGesture(Gesture *gesture);
    int contentWidth() const;
    int contentHeight() const;
    bool autoScale() const;
    void setAutoScale(bool enable);
    int lineWidth() const;
    void setLineWidth(int width);
    QColor lineColor() const;
    void setLineColor(QColor color);
    bool playing() const;
    void setPlaying(bool playing);

public slots:
    void rescale(QRectF rect = QRectF());
    void setGestureAsQObject(QObject *gesture);
    bool play();
    void pause();
    void stop();
    void clear();

protected: // From QDeclarativeItem
    void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);

protected:
    void drawNoGestureIndicator(QPainter *painter) const;
    void drawGesture(QPainter *painter, int lastPointIndexToDraw = -1);
    void drawDirectionIndicator(QPainter *painter, int currentIndex);
    inline void scalePoint(QPoint &point);

protected slots:
    void animate();
    void startAnimationsFromTheBeginning();
    void pointsChanged();

signals: // Property changed signals
    void gestureChanged(Gesture *gesture);
    void contentWidthChanged(int width);
    void contentHeightChanged(int height);
    void playingChanged(bool playing);

protected: // Data
    Gesture *m_gesture; // Not owned
    GestureDataHelper m_dataHelper;
    QTimer m_animationTimer;
    int m_animationIndex;
    AnimationStates m_animationState;
    qreal m_scaleRatio;
    bool m_autoScale;
    int m_contentWidth;
    int m_contentHeight;
    bool m_drawNoGestureIndicator;
    int m_lineWidth;
    QColor m_lineColor;
};


#endif // GESTUREVISUALIZER_H

// End of file.
